package com.sls.web.manager.action.game;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.common.FilterStringSpaces;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.EbaimiCheckFileInfo;
import com.sls.web.manager.game.pojo.EbaimiProduct;
import com.sls.web.manager.game.querybean.EbaimiProductQuery;
import com.sls.web.manager.game.service.EbaiMiService;
import com.sls.web.manager.page.Pagination;

@Results(value = { @Result(name = "filedownload", type = "stream", params = {
		"contentType", "application/x-download", "inputName", "istream",
		"contentDisposition", "attachment;filename=${fileName}" }) })
public class EbaiMiAction extends SuperAction {

	private static final long serialVersionUID = 4523430272778003349L;
	@Resource(name = "ebaiMiService")
	private EbaiMiService ebaiMiService;

	private Pagination<EbaimiProductQuery> page;
	private EbaimiProductQuery proQuery;
	private EbaimiProduct proInfo;
	private String orderId;
	private String id;
	private String supplierId;
	private String priceRatio;

	// =================
	private String oid_goodsitem;
	private String name_goods;
	private String price;
	private String oid_biz;
	private String sup_price;
	private String insert_time;
	private String modify_time;
	private String isAudit;

	private String oid_loginid;
	private String amt_balcur;
	private String stat_goodsorder;
	private String jno_cli;

	private String billDate;
	private EbaimiCheckFileInfo ebaimiCheckFileInfo;

	private String fileName;// 文件名称
	private InputStream istream; // 输入流

	/**
	 * 对账excel文件导出
	 * 
	 * @return
	 * @throws Exception
	 */
	public String exportBill() throws Exception {
		List<EbaimiCheckFileInfo> list = null;
		try {
			list = ebaiMiService.queryProPaginationOfBill(billDate,
					getCurrentOperator());
		} catch (ManagerException e1) {
			addActionError(e1.getMessage());
			flag = false;
			return SUCCESS;
		}
		
		String filenameWeb = "账单EbaiMiBill_" + billDate + ".xls";
		try {
			createExcel(filenameWeb, list);
		} catch (IOException e) {
			addActionError("导出文件异常!");
			flag = false;
			return SUCCESS;
		}
		return "filedownload";
	}
	
	/**
	 * 菜单跳转
	 * 
	 * @return
	 */
	public String billList() {

		return "bill";
	}

	/**
	 * 对账信息同步
	 * 
	 * @return
	 * @author Wangxuegang
	 * @date 2013-05-13
	 */
	public String getCheckFile() {
		try {
			ebaiMiService.sendCheckFile(getCurrentOperator());

		} catch (Exception e) {
			addActionError("导出文件异常!");
			flag = false;
			return "bill";
		}

		return "bill";
	}

	/**
	 * 商品审核
	 * 
	 * @return
	 * @author Wangxuegang
	 * @date 2013-05-13
	 */
	public String audit() {

		try {
			int priceRatioVal = Integer.parseInt(priceRatio);
			this.ebaiMiService.confirmAudit(supplierId, priceRatioVal,
					getCurrentOperator());
			redirectionUrl = "ebai_mi!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return SUCCESS;

	}

	/**
	 * 余额查询
	 * 
	 * @return
	 * @author Wangxuegang
	 * @date 2013-05-13
	 */
	public String queryBalance() {
		String oid_loginidValue = null;
		try {
			proInfo = ebaiMiService.queryOid_loginid(getCurrentOperator());
			oid_loginidValue = proInfo.getOid_loginid();
			if (StringUtils.isNotEmpty(oid_loginidValue)) {
				oid_loginidValue = oid_loginidValue.trim();
			}
			proQuery.setOid_loginid(oid_loginidValue);

			proInfo = ebaiMiService
					.queryBalance(proQuery, getCurrentOperator());
			String retcode = proInfo.getRetcode();
			if ("000000".equals(retcode)) {
			} else {
				flag = false;
				addActionMessage(proInfo.getRetmsg());
			}

		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		proInfo.setOid_loginid(oid_loginidValue);
		
		return "balance_query";
	}

	/**
	 * 二级菜单--页面跳转<br>
	 * 合作方的易百米商户ID(目前为11位)
	 * 
	 * @return
	 * @author Wangxuegang
	 * @date 2013-05-13
	 */
	public String toBalance() {
		try {
			proInfo = ebaiMiService.queryOid_loginid(getCurrentOperator());

			return "balance_query";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}

		return SUCCESS;
	}

	/**
	 * 订单结果查询
	 * 
	 * @return
	 * @author Wangxuegang
	 * @date 2013-05-13
	 */
	public String queryOrderStatus() {
		try {
			if (proQuery == null) {
				proQuery = new EbaimiProductQuery();
				jno_cli = proQuery.getJno_cli();
				jno_cli = FilterStringSpaces.checkStringHasSpace(jno_cli);
				proQuery.setJno_cli(jno_cli);
			}
			proInfo = ebaiMiService.queryOrder(proQuery, getCurrentOperator());
			jno_cli = proQuery.getJno_cli();
			jno_cli = FilterStringSpaces.checkStringHasSpace(jno_cli);
			proInfo.setJno_cli(jno_cli);
			return "order_query";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}

		return SUCCESS;
	}

	/**
	 * 二级菜单--订单查询页面跳转
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toOrder() throws Exception {
		return "order_query";
	}

	/**
	 * 商品删除
	 * 
	 * @return
	 * @author Wangxuegang
	 * @date 2013-05-13
	 */
	public String delete() {
		try {
			ebaiMiService.deleteGoods(id, getCurrentOperator());

			if (proQuery == null) {
				proQuery = new EbaimiProductQuery();
				proQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
				proQuery.setPageSize(Constants.INIT_PAGESIZE);
			}
			if (StringUtils.isNotEmpty(oid_goodsitem)) {
				proQuery.setOid_goodsitem(oid_goodsitem);
			}
			if (StringUtils.isNotEmpty(name_goods)) {
				proQuery.setName_goods(name_goods);
			}
			if (StringUtils.isNotEmpty(price)) {
				proQuery.setPrice(price);
			}
			if (StringUtils.isNotEmpty(oid_biz)) {
				proQuery.setOid_biz(oid_biz);
			}
			if (StringUtils.isNotEmpty(sup_price)) {
				proQuery.setSup_price(sup_price);
			}
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}

		return list();
	}

	/**
	 * 商品列表--分页展示
	 * 
	 * @return
	 * @throws Exception
	 * @author Wangxuegang
	 * @date 2013-05-11
	 */
	public String list() {
		if (proQuery == null) {
			proQuery = new EbaimiProductQuery();
			proQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			proQuery.setPageSize(Constants.INIT_PAGESIZE);
		}
		try {
			page = this.ebaiMiService.queryProPagination(proQuery,
					getCurrentOperator());
		} catch (ManagerException e) {
			this.addActionError(e.getMessage());
		}
		return LIST;
	}

	/**
	 * 商品新增页面跳转
	 * 
	 * @return
	 * @author Wangxuegang
	 * @date 2013-05-11
	 */
	public String add() {
		return INPUT;
	}

	/**
	 * 商品新增--页面提交
	 * 
	 * @return
	 * @author Wangxuegang
	 * @date 2013-05-11
	 */
	public String saveGoodsInfo() {
		try {
			ebaiMiService.saveGoods(proInfo, getCurrentOperator());
			redirectionUrl = "ebai_mi!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}

		return SUCCESS;
	}

	/**
	 * 商品编辑--页面提交
	 * 
	 * @return
	 * @author Wangxuegang
	 * @date 2013-05-11
	 */
	public String editGoodsInfo() {
		try {
			ebaiMiService.updateGoods(proInfo, getCurrentOperator());
			if (proQuery == null) {
				proQuery = new EbaimiProductQuery();
				proQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
				proQuery.setPageSize(Constants.INIT_PAGESIZE);
			}
			if (StringUtils.isNotEmpty(oid_goodsitem)) {
				proQuery.setOid_goodsitem(oid_goodsitem);
			}
			if (StringUtils.isNotEmpty(name_goods)) {
				proQuery.setName_goods(name_goods);
			}
			if (StringUtils.isNotEmpty(price)) {
				proQuery.setPrice(price);
			}
			if (StringUtils.isNotEmpty(oid_biz)) {
				proQuery.setOid_biz(oid_biz);
			}
			if (StringUtils.isNotEmpty(sup_price)) {
				proQuery.setSup_price(sup_price);
			}
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}

		return list();
	}

	/**
	 * 商品编辑
	 * 
	 * @return
	 * @throws Exception
	 * @author Wangxuegang
	 * @date 2013-05-11
	 */
	public String toEditPro() throws Exception {
		try {
			proInfo = this.ebaiMiService.queryById(id, getCurrentOperator());
		} catch (ManagerException e) {
			this.addActionError(e.getMessage());
		}
		return INPUT;
	}
	
	/**
	 * 
	 * @param filenameWeb
	 * @param list
	 * @throws IOException
	 */
	public void createExcel(String filenameWeb, List<EbaimiCheckFileInfo> list)
			throws IOException {
		// 第一步，创建一个webbook，对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		// 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet("账单信息");
		// 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
		HSSFRow row = sheet.createRow(0);
		// 第四步，创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
		HSSFCellStyle style2 = wb.createCellStyle();
		style2.setAlignment(HSSFCellStyle.ALIGN_RIGHT);// 创建一个居右格式
		HSSFCell cell = row.createCell(0);
		cell.setCellValue("ebaimi订单号");
		cell.setCellStyle(style);
		cell = row.createCell(1);
		cell.setCellValue("订单创建时间");
		cell.setCellStyle(style);
		cell = row.createCell(2);
		cell.setCellValue("商品名称");
		cell.setCellStyle(style);
		cell = row.createCell(3);
		cell.setCellValue("代理商流水号");
		cell.setCellStyle(style);
		cell = row.createCell(4);
		cell.setCellValue("代理商订单创建时间");
		cell.setCellStyle(style);
		cell = row.createCell(5);
		cell.setCellValue("数量");
		cell.setCellStyle(style);
		cell = row.createCell(6);
		cell.setCellValue("交易额（元）");
		cell.setCellStyle(style);
		cell = row.createCell(7);
		cell.setCellValue("状态");
		cell.setCellStyle(style);
		// 第五步，写入实体数据 实际应用中这些数据从数据库得到
		if (list != null && !list.isEmpty()) {
			for (int i = 0; i < list.size(); i++) {
				row = sheet.createRow((int) i + 1);
				EbaimiCheckFileInfo bill = (EbaimiCheckFileInfo) list.get(i);
				
				// 第四步，创建单元格，并设置值
				cell = row.createCell(0);
				cell.setCellStyle(style);
				cell.setCellValue(bill.getOid_goodsorder());
				cell = row.createCell(1);
				cell.setCellStyle(style);
				cell.setCellValue(bill.getDt_create());
				cell = row.createCell(2);
				cell.setCellStyle(style);
				cell.setCellValue(bill.getGoodsName());
				cell = row.createCell(3);
				cell.setCellStyle(style);
				cell.setCellValue(bill.getJno_cli());
				cell = row.createCell(4);
				cell.setCellStyle(style);
				cell.setCellValue(bill.getAgent_dt_create());
				cell = row.createCell(5);
				cell.setCellStyle(style);
				cell.setCellValue(bill.getBuyNum());
				cell = row.createCell(6);
				cell.setCellStyle(style);
				cell.setCellValue(bill.getTradeMoney());
				cell = row.createCell(7);
				cell.setCellStyle(style);
				cell.setCellValue(bill.getStatus());
			}
		}
		
		// 第六步，将文件存到指定位置
		fileName = new String(filenameWeb.getBytes("GBK"), "iso8859-1");	
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		wb.write(baos);
		istream = new ByteArrayInputStream(baos.toByteArray());
		baos.close();
	}

	public String editPro() throws Exception {
		try {
			this.ebaiMiService.updatePro(proInfo, getCurrentOperator());

			if (proQuery == null) {
				proQuery = new EbaimiProductQuery();
				proQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
				proQuery.setPageSize(Constants.INIT_PAGESIZE);
			}
			if (StringUtils.isNotEmpty(oid_goodsitem)) {
				proQuery.setOid_goodsitem(oid_goodsitem);
			}
			if (StringUtils.isNotEmpty(name_goods)) {
				proQuery.setName_goods(name_goods);
			}

		} catch (ManagerException e) {
			this.addActionError(e.getMessage());
		}
		return list();
	}

	public String sync() {
		try {
			this.ebaiMiService.sync(getCurrentOperator());
			this.redirectionUrl = "ebai_mi!list.htm";
		} catch (ManagerException e) {
			flag = false;
			this.addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	public String toQueryBalance() {
		return "balance";
	}

	public String toQueryOrder() {
		return "order";
	}

	public String queryOrder() {

		return "order";
	}

	/**
	 * 批量上架
	 * 
	 * @return
	 * @throws Exception
	 */
	public String batchOnline() throws Exception {
		try {
			this.ebaiMiService.batchOnline("1", getCurrentOperator());
			redirectionUrl = "ebai_mi!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 批量下架
	 * 
	 * @return
	 * @throws Exception
	 */
	public String batchOffline() throws Exception {
		try {
			this.ebaiMiService.batchOffline("0", getCurrentOperator());
			redirectionUrl = "ebai_mi!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	public Pagination<EbaimiProductQuery> getPage() {
		return page;
	}

	public void setPage(Pagination<EbaimiProductQuery> page) {
		this.page = page;
	}

	public EbaimiProductQuery getProQuery() {
		return proQuery;
	}

	public void setProQuery(EbaimiProductQuery proQuery) {
		this.proQuery = proQuery;
	}

	public EbaimiProduct getProInfo() {
		return proInfo;
	}

	public void setProInfo(EbaimiProduct proInfo) {
		this.proInfo = proInfo;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getSupplierId() {
		return supplierId;
	}

	public void setSupplierId(String supplierId) {
		this.supplierId = supplierId;
	}

	public String getPriceRatio() {
		return priceRatio;
	}

	public void setPriceRatio(String priceRatio) {
		this.priceRatio = priceRatio;
	}

	public String getOid_goodsitem() {
		return oid_goodsitem;
	}

	public void setOid_goodsitem(String oid_goodsitem) {
		this.oid_goodsitem = oid_goodsitem;
	}

	public String getName_goods() {
		return name_goods;
	}

	public void setName_goods(String name_goods) {
		this.name_goods = name_goods;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public String getOid_biz() {
		return oid_biz;
	}

	public void setOid_biz(String oid_biz) {
		this.oid_biz = oid_biz;
	}

	public String getSup_price() {
		return sup_price;
	}

	public void setSup_price(String sup_price) {
		this.sup_price = sup_price;
	}

	public String getInsert_time() {
		return insert_time;
	}

	public void setInsert_time(String insert_time) {
		this.insert_time = insert_time;
	}

	public String getModify_time() {
		return modify_time;
	}

	public void setModify_time(String modify_time) {
		this.modify_time = modify_time;
	}

	public String getIsAudit() {
		return isAudit;
	}

	public void setIsAudit(String isAudit) {
		this.isAudit = isAudit;
	}

	public String getOid_loginid() {
		return oid_loginid;
	}

	public void setOid_loginid(String oid_loginid) {
		this.oid_loginid = oid_loginid;
	}

	public String getAmt_balcur() {
		return amt_balcur;
	}

	public void setAmt_balcur(String amt_balcur) {
		this.amt_balcur = amt_balcur;
	}

	public String getStat_goodsorder() {
		return stat_goodsorder;
	}

	public void setStat_goodsorder(String stat_goodsorder) {
		this.stat_goodsorder = stat_goodsorder;
	}

	public String getJno_cli() {
		return jno_cli;
	}

	public void setJno_cli(String jno_cli) {
		this.jno_cli = jno_cli;
	}

	public String getBillDate() {
		return billDate;
	}

	public void setBillDate(String billDate) {
		this.billDate = billDate;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public InputStream getIstream() {
		return istream;
	}

	public void setIstream(InputStream istream) {
		this.istream = istream;
	}

	public EbaimiCheckFileInfo getEbaimiCheckFileInfo() {
		return ebaimiCheckFileInfo;
	}

	public void setEbaimiCheckFileInfo(EbaimiCheckFileInfo ebaimiCheckFileInfo) {
		this.ebaimiCheckFileInfo = ebaimiCheckFileInfo;
	}

}
